package org.wlgzs.shop.service.impl;

import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.servlet.ModelAndView;
import org.wlgzs.shop.bean.*;
import org.wlgzs.shop.dao.GoodsDataMapper;
import org.wlgzs.shop.dao.OrderTransportDataMapper;
import org.wlgzs.shop.dao.StoreMapper;
import org.wlgzs.shop.dao.UserDateMapper;
import org.wlgzs.shop.service.IGoodsDateService;
import org.wlgzs.shop.service.IOrderTransportDateService;
import org.wlgzs.shop.service.IUserDateService;
import org.wlgzs.shop.service.ManagerService;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.List;

@Service("managerService")
public class ManagerServiceImpl implements ManagerService {
    @Resource(name = "GoodsDateService")
    IGoodsDateService goodsDateService;
    @Resource(name = "OrderTransportDateService")
    IOrderTransportDateService orderTransportDateService;
    @Resource(name = "userDateService")
    IUserDateService userDateService;
    @Autowired
    UserDateMapper userDateMapper;
    @Autowired
    GoodsDataMapper goodsDataMapper;
    @Autowired
    OrderTransportDataMapper orderTransportDataMapper;
    @Autowired
    StoreMapper storeMapper;
    /**
     *
     * @param date
     * @return Msg
     *
     * 添加管理者
     */
    @Override
    public Msg addManager(UserDate date) {
        date.setPower(1);
        userDateMapper.insert(date);
        return Msg.success().add("addManager","添加管理者成功！");
    }

    /**
     *
     * @param userId
     * @return Msg
     *
     * 删除用户
     */
    @Override
    public Msg delete(int userId) {
        userDateMapper.deleteByPrimaryKey(userId);
        return Msg.success().add("deleteUser","删除用户成功！");
    }

    //添加用户
    @Override
    public boolean addUser(UserDate date) {
        if(userDateService.selectSameName(date.getUserName())!=null&&userDateService.selectSameName(date.getUserName()).size()!=0){
            System.out.println("用户名已存在！");
            return false;
    }
        int number=userDateMapper.insert(date);
        System.out.println(number);
       if(number!=0) {
           return true;
       }
       else{
           return false;
       }
    }

    //修改管理者密码
    @Override
    public Msg  setManagerPass(UserDate date, HttpSession session) {
        UserDate manager = (UserDate) session.getAttribute("user");
        if(date==null){
            return Msg.fail().add("errors","获取修改信息失败！");
        }
        else if (date.getPassword().equals("")||date.getPassword()!=manager.getPassword()){
            return Msg.fail().add("errors","修改信息不可为空！");
        }
        else {
            date.setUserId(manager.getUserId());
            userDateMapper.updateByPrimaryKey(date);
            session.invalidate();
            return Msg.success().add("win", "修改完成");
    }
}

    //管理者登陆
    @Override
    public Msg loginManager(String account, String password, HttpSession session) {
        if(account==null||password==null){
            return Msg.fail().add("errors","账号或密码不能为空！");
        }
        UserDate date=userDateService.userAccountSelectUser(account);
        if(date==null||date.getPower()!=1){
            return Msg.fail().add("errors","管理者不存在！");
        }
        else if(!password.equals(date.getPassword())){
            return Msg.fail().add("errors","密码或账号错误！");
        }
         else {
             session.setAttribute("manager",date);
             return Msg.success().add("win","登陆成功！");
        }
    }

    //选择所有商品
    @Override
    public List<GoodsData> selectAllGoodsDate() {
        return goodsDataMapper.selectByExample(null);
    }

    //选择所有用户
    @Override
    public List<UserDate> selectAllUserDate() {
        return userDateMapper.selectByExample(null);
    }

    //选择所有订单
    @Override
    public List<OrderTransportData> selectAllOrderDate() {
        return  orderTransportDateService.allOrder();
    }

    //查询所有店铺
    @Override
    public List<Store> selectAllStore() {
        return storeMapper.selectByExample(null);
    }

    //修改商品信息
    @Override
    public Msg setGoodsDate(GoodsData goodsData) {
        if(goodsData==null){
            return Msg.fail().add("error","修改信息失败!");
        }
        else {
            goodsDataMapper.updateByPrimaryKeySelective(goodsData);
            return Msg.success().add("setGoods", "修改商品信息完成！");
        }
    }

    //修改用户信息
    @Override
    public Msg setUserDate(UserDate date) {
        if(date!=null){
            return Msg.fail().add("error","无可修改信息!");
        }
        else {
            userDateMapper.updateByPrimaryKeySelective(date);
            return Msg.success().add("setUser", "修改用户信息完成！");
        }
    }

    //修改订单信息
    @Override
    public Msg setOrderDate(OrderTransportData orderTransportData) {
        if(orderTransportData==null){
            return Msg.fail().add("error","修改信息为空！");
        }
        else{
            orderTransportDataMapper.updateByPrimaryKey(orderTransportData);
            return Msg.success().add("setUser", "修改订单信息完成！");
        }
    }

    //删除用户
    @Override
    public Msg deleteUser(int userId) {
        if(userDateMapper.deleteByPrimaryKey(userId)!=0) {
            return Msg.success().add("deleteUser", "删除用户成功！");
        }
        else{
            return Msg.fail().add("errors","无该用户id");
        }
    }

    //删除订单
    @Override
    public Msg deleteOrder(int orderId) {
        if(orderTransportDataMapper.deleteByPrimaryKey(orderId)>0) {
            return Msg.success().add("deleteOrder", "订单删除成功");
        }
        else{
            return Msg.fail().add("errors","无该订单id");
        }
    }

    //删除商品
    @Override
    public Msg deleteGoods(int goodsId) {
        if(goodsDataMapper.deleteByPrimaryKey(goodsId)>0) {
            return Msg.success().add("deleteGoods", "商品删除成功！");
        }
        else{
            return Msg.fail().add("errors","无该商品id");
        }
    }

    //查询用户信息
    @Override
    public List<UserDate> selectUser(String date) {
        return userDateMapper.selectUser2(date);
    }

    //查询订单信息
    @Override
    public List<OrderTransportData>  selectOrder(String date) {
    return orderTransportDataMapper.selectOrder2(date);
    }

    //查询商品信息
    @Override
    public List<GoodsData> selectGoods(String date) {
        return goodsDataMapper.selectGoods2(date);
    }

    @Override
    public List<UserDate> selectAllLikeDate(String date) {
        UserDateExample userDateExample=new UserDateExample();
        UserDateExample.Criteria criteria=userDateExample.createCriteria();
        try {
            criteria.andUserIdEqualTo(Integer.parseInt(date));
        } catch (NumberFormatException e) {
        }
        userDateExample.or().andUserNameLike("%"+date+"%");
        return userDateMapper.selectByExample(userDateExample);
    }

    //店铺查询
    @Override
    public List<Store> selectAllStoreLikeDate(String date) {
        StoreExample storeExample=new StoreExample();
        StoreExample.Criteria criteria=storeExample.createCriteria();
        try {
            criteria.andStoreIdEqualTo(Integer.parseInt(date));
        } catch (NumberFormatException e) {
        }
        storeExample.or().andStoreNameLike("%"+date+"%");
        return storeMapper.selectByExample(storeExample);
    }

    //商品查询
    @Override
    public List<GoodsData> selectAllGoodsLikeDate(String date) {
        GoodsDataExample goodsDataExample=new GoodsDataExample();
        GoodsDataExample.Criteria criteria=goodsDataExample.createCriteria();
        try {
            criteria.andIdEqualTo(Integer.parseInt(date));
        } catch (NumberFormatException e) {
        }
        goodsDataExample.or().andGoodsNameLike("%"+date+"%");
        return goodsDataMapper.selectByExample(goodsDataExample);
    }

    @Override
    public Msg upUser(UserDate userDate) {
        int number=userDateMapper.updateByPrimaryKeySelective(userDate);
        if(number!=0){
            return Msg.success();
        }
        return Msg.success();
    }

    @Override
    public Msg upGoods(GoodsData goodsData) {
        int number=goodsDataMapper.updateByPrimaryKeySelective(goodsData);
        if(number!=0){
            return Msg.success();
        }
        return Msg.success();
    }

    @Override
    public Msg upShop(Store store) {
        int number=storeMapper.updateByPrimaryKeySelective(store);
        if(number!=0){
            return Msg.success();
        }
        return Msg.success();
    }

    @Override
    public Msg addShop(String storeName,String id,String storeKind) {
        SimpleDateFormat simpleDateFormat=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Calendar calendar=Calendar.getInstance();
        String timeDate=simpleDateFormat.format(calendar.getTime());
        Store store=new Store();
        store.setSetTime(timeDate);
        store.setStoreName(storeName);
        int userId=Integer.parseInt(id);
        try {
            store.setUserId(userId);
        } catch (Exception e) {
            e.printStackTrace();
            return Msg.fail();
        }
        store.setCollectionNumber(0);
        store.setReviewPower(0);
        store.setStoreKind(storeKind);
        int number=storeMapper.insert(store);
        if(number!=0){
            return Msg.success();
        }
        return Msg.fail();
    }

    @Override
    public Msg delectShop(int id) {
        int number=storeMapper.deleteByPrimaryKey(id);
        if(number!=0){
            return Msg.success();
        }
        return Msg.fail();
    }


}
